package com.erp.framework.controller.taglib;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.jsp.JspException;

import com.erp.framework.model.dao.ServiceLocator;
import com.erp.framework.util.logger.Logger;

public class CustomOptionsTag extends javax.servlet.jsp.tagext.TagSupport {

    private String table = null;
    private String valuecolumn = null;
    private String textcolumn = null;
    private String where = null;
    private String group = null;
    private String order = null;
    private String selected = null;

    public void setTable(String text) {

        this.table = text;
    }

    public void setValuecolumn(String text) {

        this.valuecolumn = text;
    }

    public void setTextcolumn(String text) {

        this.textcolumn = text;
    }

    public void setWhere(String text) {

        this.where = text;
    }

    public void setOrder(String text) {

        this.order = text;
    }

    public void setGroup(String text) {

        this.group = text;
    }

    public void setSelected(String text) {

        this.selected = text;
    }

    public int doEndTag() throws JspException {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        StringBuffer rslt = new StringBuffer();
        String query = "";

        try {
            query = " SELECT " + valuecolumn + " COLA," + textcolumn + " COLB FROM " + table
                    + ((where != null && !where.equals("")) ? " WHERE " + where : "")
                    + ((group != null && !group.equals("")) ? " GROUP BY " + group : "")
                    + ((order != null && !order.equals("")) ? " ORDER BY " + order : "");

            String valueStr = null;
            conn = ServiceLocator.getInstance().getDSDBConnection();
            stmt = conn.createStatement();

            Logger.all(this, "doEndTag(), sql=" + query);
            rs = stmt.executeQuery(query);

            if (rs != null) {
                selected = (selected == null) ? "" : selected.trim();
                while (rs.next()) {
                    valueStr = (rs.getString(1) == null) ? "" : rs.getString(1).trim();

                    rslt.append("<OPTION VALUE='" + valueStr + "'");
                    if (valueStr.equals(selected))
                        rslt.append(" SELECTED");
                    rslt.append(">");
                    rslt.append((rs.getString(2) == null) ? "" : rs.getString(2));
                    rslt.append("</OPTION>\n");
                }
            }

            Logger.all(this, "doEndTag(), html=" + rslt.toString());
            pageContext.getOut().print(rslt.toString());
        } catch (Exception e) {
            Logger.logExceptions(this, e);
            Logger.error("doEndTag(), html=" + rslt.toString());
            Logger.error("doEndTag(), sql=" + query);
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (stmt != null)
                    stmt.close();
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                Logger.logExceptions(this, e);
            }
        }

        return EVAL_PAGE;
    }
}